package com.lenovo.smarttraffic.ui.activity.stage3;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;

import com.lenovo.smarttraffic.Constant;
import com.lenovo.smarttraffic.R;
import com.lenovo.smarttraffic.ui.activity.BaseActivity;
import com.lenovo.smarttraffic.util.ToastUtils;

public class EnvThresholdActivity extends BaseActivity {

    private Switch alert_switch;

    private EditText temp_et;
    private EditText hum_et;
    private EditText light_et;
    private EditText co2_et;
    private EditText pm25_et;
    private EditText road_et;

    private Button save_btn;

    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;

    @Override
    protected int getLayout() {
        return R.layout.activity_env_threshold;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        preferences = getSharedPreferences(Constant.THRESHOLD, MODE_PRIVATE);
        editor = preferences.edit();
        initView();
        initData();
    }

    public void initView() {
        initToolBar(this, "阈值设置");

        alert_switch = findViewById(R.id.alert_switch);

        temp_et = findViewById(R.id.temp_et);
        temp_et.setText(preferences.getString(Constant.ts_temp, ""));
        hum_et = findViewById(R.id.hum_et);
        hum_et.setText(preferences.getString(Constant.ts_hum, ""));
        light_et = findViewById(R.id.light_et);
        light_et.setText(preferences.getString(Constant.ts_light, ""));
        co2_et = findViewById(R.id.co2_et);
        co2_et.setText(preferences.getString(Constant.ts_co2, ""));
        pm25_et = findViewById(R.id.pm25_et);
        pm25_et.setText(preferences.getString(Constant.ts_pm25, ""));
        road_et = findViewById(R.id.road_et);
        road_et.setText(preferences.getString(Constant.ts_road, ""));

        save_btn = findViewById(R.id.save_btn);
    }


    public void initData() {
        alert_switch.setOnCheckedChangeListener((buttonView, isChecked) -> toggle(isChecked));
        alert_switch.setChecked(preferences.getBoolean(Constant.AUTO_ALERT, true));
        toggle(alert_switch.isChecked());


        save_btn.setOnClickListener(v -> {
            if (isEmpty(temp_et) && isEmpty(hum_et) && isEmpty(light_et) && isEmpty(co2_et) && isEmpty(pm25_et) && isEmpty(road_et)) {
                ToastUtils.showToast(this, "请输入所有内容");
            } else {
                editor.putString(Constant.ts_temp, temp_et.getText().toString()).apply();
                editor.putString(Constant.ts_hum, hum_et.getText().toString()).apply();
                editor.putString(Constant.ts_light, light_et.getText().toString()).apply();
                editor.putString(Constant.ts_co2, co2_et.getText().toString()).apply();
                editor.putString(Constant.ts_pm25, pm25_et.getText().toString()).apply();
                editor.putString(Constant.ts_road, road_et.getText().toString()).apply();
                ToastUtils.showToast(this, "保存成功");
            }
        });
    }

    private boolean isEmpty(EditText editText) {
        return editText.getText().toString().isEmpty();
    }

    private void toggle(boolean isChecked) {
        alert_switch.setText("自动报警：" + (isChecked ? "开" : "关"));
        preferences.edit().putBoolean(Constant.AUTO_ALERT, isChecked).apply();

        temp_et.setEnabled(!isChecked);
        hum_et.setEnabled(!isChecked);
        light_et.setEnabled(!isChecked);
        co2_et.setEnabled(!isChecked);
        pm25_et.setEnabled(!isChecked);
        road_et.setEnabled(!isChecked);
        save_btn.setEnabled(!isChecked);
    }

}
